#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
APP="virt-manager"
APP_NAME="Virtual Machine Manager"
APP_GROUP="libvirt"

# install packages
pkg update
pkg install \
    qemu-kvm \
    libvirt-clients \
    libvirt-daemon-system \
    bridge-utils \
    virt-manager \
    gir1.2-spiceclientgtk-3.0

cat <<EOF >/usr/bin/$APP-allowuser.sh
#!/bin/bash
if ! grep $APP_GROUP /etc/group | grep \$(id -nu 1000) >/dev/null; then
    usermod -a -G $APP_GROUP \$(id -nu 1000)
fi
EOF
chmod +x /usr/bin/$APP-allowuser.sh
cat <<EOF >/usr/lib/systemd/system/$APP-allowuser.service
[Unit]
Description=Allow user to use $APP_NAME
#After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/$APP-allowuser.sh
RemainAfterExit=true
ExecStop=
StandardOutput=journal

[Install]
WantedBy=multi-user.target
EOF
systemctl enable $APP-allowuser.service

PACKAGE="virt-manager"
echo "PACKAGE=$PACKAGE" >/.package
echo "VERSION=$(dpkg -s $PACKAGE | grep Version | sed 's/Version: //g' | sed 's/:/-/g')" >>/.package
